<?php
/**
 * Free Uch
 *
 * @category   FreeUch
 * @copyright  Copyright 2009 lordharrdlee(http://www.lordharrdlee.cn)
 * @author     lordharrdlee<lordharrdlee@gmail.com>
 * @version    $Id: Repaste.php 10 2010-01-14 10:31:01Z lordharrdlee $
 */
/**
 * Model_Repaste
 * @author liyizhou<lordharrdlee@hotmail.com>
 */
class Model_Repaste extends Model_ZBase {

    private $_perpage = 10;
    private $_page = 1;

    public function init() {
        $this->_name = tname('freeuch_repaste');
    }

    public function setPerpage($perpage) {
        $this->_perpage = $perpage;
    }

    public function setPage($page) {
        $this->_page = $page;
    }

    public function getListSelect() {
        $select = $this->_db->select();
        $select->from(array('rm'=>$this->_name.'map'))
               ->joinLeft(array('r'=>$this->_name), 'rm.rpid=r.rpid')
               ->order('rm.rpmid DESC')
               ->limitPage($this->_page, $this->_perpage);
        return $select;
    }

    public function getListAll() {
        return $this->_db->fetchAll($this->getListSelect());
    }

    public function getListByUid($uid) {
        return $this->_db->fetchAll($this->getListSelect()->where('rm.uid=?',$uid));
    }

    public function getListFriends($uid) {
        $select = $this->_db->select();
        $select->from(array('f'=>tname('friend')),'')
               ->joinLeft(array('rm'=>$this->_name.'map'), 'f.uid = \''.$uid.'\' AND f.fuid = rm.uid')
               ->joinLeft(array('r'=>$this->_name), 'rm.rpid=r.rpid')
               ->where('rm.uid IS NOT NULL')
               ->order('rm.rpmid DESC')
               ->limitPage($this->_page, $this->_perpage);
        return $this->_db->fetchAll($select);
    }

    public function getOne($rpmid) {
        $select = $this->_db->select();
        $select->from(array('rm'=>$this->_name.'map'))
               ->joinLeft(array('r'=>$this->_name),'rm.rpid=r.rpid')
               ->joinLeft(array('rf'=>$this->_name.'field'), 'rm.rpid=rf.rpid')
               ->where('rm.rpmid=?',$rpmid);
        return $this->_db->fetchRow($select);
    }

    public function addNew($data) {
        $datafield = array(
            'content'=>$data['content'],
            'hasvote'=>$data['hasvote']
        );
        $datamap = array(
            'uid'=>$data['authorid'],
            'username'=>$data['author'],
            'friend'=>$data['friend'],
            'target_ids'=>$data['target_ids'],
            'time'=>$data['dateline']
        );
        unset($data['content']);
        unset($data['hasvote']);
        unset($data['friend']);
        unset($data['target_ids']);
        $rpid = $this->insert($data);
        $datafield['rpid'] = $datamap['rpid'] = $rpid;
        $this->_db->insert($this->_name.'field', $datafield);
        $this->addRepasteMap($datamap);
    }

    public function addRepaste($data) {
        $repaste = $this->fetchRow($this->select()->where('rpid=?',$data['rpid']));
        $this->addRepasteMap($data);
        $repaste->rpnum += 1;
        $repaste->save();
    }

    public function addRepasteMap($data) {
        $repastemap = new Model_RepasteMap();
        $newrow = $repastemap->createRow($data);
        $newrow->save();
    }

}